package com.bbcloud.service.impl;

import com.bbcloud.common.ServerResponse;
import com.bbcloud.common.TokenCache;
import com.bbcloud.dao.CartMapper;
import com.bbcloud.dao.ProductMapper;
import com.bbcloud.dao.UserMapper;
import com.bbcloud.pojo.Cart;
import com.bbcloud.pojo.Product;
import com.bbcloud.pojo.User;
import com.bbcloud.service.IShopService;
import com.bbcloud.vo.CartProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuzhu on 2018/12/11.
 */
@Service("iShopService")
public class ShopServiceImpl implements IShopService{

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CartMapper cartMapper;

    public ServerResponse<PageInfo> list(int pageNum, int pageSize, String orderBy){
        PageHelper.startPage(pageNum,pageSize);
        List<Product> productList = productMapper.selectAllProduct();
        PageInfo pageResult = new PageInfo(productList);
        pageResult.setList(productList);
        return ServerResponse.createBySuccess(pageResult);
    }

    public ServerResponse addToCart(String token, String userId,String productId,int quantity) {
        User user = userMapper.selectUser(userId);
        if (user == null){
            return ServerResponse.createByErrorMessage("The user doesn't exist.");
        }
        String tokenValid = TokenCache.getKey(TokenCache.TOKEN_PREFIX+user.getEmail());
        if (!StringUtils.equals(tokenValid,token)){
            return ServerResponse.createByErrorMessage("Please log in again");
        }
        int rowCount = cartMapper.isProductAlreadyAdded(userId,productId);
        if (rowCount>0){
            return ServerResponse.createByErrorMessage("product already exis!");
        }
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setProductId(productId);
        cart.setChecked(1);
        cart.setQuantity(quantity);
        rowCount = cartMapper.insert(cart);
        if (rowCount > 0){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("failed to cart");
    }

    public ServerResponse<PageInfo> cartProductList(String token, String userId,int pageNum, int pageSize, String orderBy) {
        User user = userMapper.selectUser(userId);
        if (user == null) {
            return ServerResponse.createByErrorMessage("The user doesn't exist.");
        }
        String tokenValid = TokenCache.getKey(TokenCache.TOKEN_PREFIX + user.getEmail());
        if (!StringUtils.equals(tokenValid, token)) {
            return ServerResponse.createByErrorMessage("Please log in again");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Cart> cartList = cartMapper.selectAllCart(userId);
        List<CartProductVo> cartProductVoList = Lists.newArrayList();
        for (Cart cart : cartList) {
            CartProductVo cartProductVo = assembleCartProductListVo(cart);
            cartProductVoList.add(cartProductVo);
        }
        PageInfo pageResult = new PageInfo(cartList);
        pageResult.setList(cartProductVoList);
        return ServerResponse.createBySuccess(pageResult);
    }

    private CartProductVo assembleCartProductListVo(Cart cart) {

        CartProductVo cartProductVo = new CartProductVo();
        cartProductVo.setProductId(cart.getProductId());
        Product product = productMapper.selectProductByProductId(cart.getProductId());
        cartProductVo.setCategoryId(product.getCategoryId());
        cartProductVo.setProductName(product.getProductName());
        cartProductVo.setPrice(product.getPrice());
        cartProductVo.setProductImages(product.getProductImages());
        return cartProductVo;
    }
}
